Tween = Tween or Class("Tween")

---@diagnostic disable-next-line: duplicate-set-field
function Tween:ctor(target)
    self.actions = {}
    self.target = target
    TweenSystem:add(self)
end
function Tween:to(duration, props, opts)
    local action = Action.new(duration, props, opts)
    table.insert(self.actions, action)
    return self
end

function Tween:start()
    self.finalAction = self.finalAction or self:_union()
    self.finalAction:start(self.target)
    self.started = true
    return self
end

function Tween:delay(duration)
    local action = Action.new(duration)
    table.insert(self.actions, action)
    return self
end

function Tween:call(fn)
    local action = Action.new(0, {}, {callback = fn})
    table.insert(self.actions, action)
    return self
end

function Tween:delayCall(duration, fn)
    self:delay(duration)
    self:call(fn)
    return self
end

function Tween:union()
    local action = self:_union()
    self.actions = {action}
    return self
end

function Tween:_union()
    return #self.actions == 1 and self.actions[1] or ActionSequeue.new(self.actions)
end

function Tween:update(dt)
    if not self.started then
        return
    end

    -- 播放结束
    if self.finalAction:update(dt) then
        self.completed = true
    end
end